$(function () {
    loadGridTable1_2();
});

$(function () {
    $('input[name=intell_switch]').click(function() {
        var beChecked = $('input[name=intell_switch]:checked').val();
        if(beChecked=='intell_on'){
          $('#intellItems').show();
          $("#intelligence_label_1").attr('disabled',false);
          $('#num_1').spinner('enable');
          $('#frequency').spinner('enable');
        }else{
          $('#intellItems').hide();
          $("#intelligence_label_1").attr('disabled',true);
          $('#num_1').spinner('disable');
          $('#frequency').spinner('disable');
        }
     });
});


$(function() {
  $('#item_key').keypress(function(event) {
    if ((event.keyCode == 13)) {// 在这里接收的是Enter事件,S的ASCII码为13。
       doSearch1();
    }
  });
    
  $('#item_key').blur(function(){
    doSearch1();
  });
  
  $("#sellerCats").change(function () {
    doSearch1();
  });
 
  $("#approve_status").change(function () {
    doSearch1();
  });
});

$(function () {
  $("#intelligence_label_1").change(function () {
    var selectd_label = $(this).val();
    $.get("ipro.action", "ipro.intelligence_label_1=" + selectd_label);
  });
  
  var version_no = $("#version_no").val();
  if(version_no=='3'){
    $('#pro4ipro_div').attr('title','');
    $('#frequency').spinner({ min: 1, max: 30 });
    $('#num_1').spinner({ min: 0, max: 50 });
    set_intell_labels_vip();
  }else if(version_no=='2'){
    set_intell_labels_normal();
    $('#frequency').spinner({ min: 3, max: 30 });
    $('#num_1').spinner({ min: 0, max: 10 });
    $("input[name=pro4ipro]").attr('disabled',true);
  }else{
    set_intell_labels_normal();
    $('#frequency').spinner({ min: 15, max: 30 });
    $('#num_1').spinner({ min: 0, max: 5 });
    $("input[name=pro4ipro]").attr('disabled',true);
  }
  
  $('#num_1').spinner().change(function(){
    $.get("ipro.action", "ipro.intelligence_num_1=" + $(this).val());
  });
  
  $('#frequency').spinner().change(function(){
    $.get("ipro.action", "ipro.intelligence_frequency=" + $(this).val());
  });
  
});

function set_intell_labels_vip(){
  var options ='';
  options+="<option value='1'>最近上架的宝贝</option>";
  options+="<option value='11'>最近编辑过的宝贝</option>";
  options+="<option value='2'>本店销量最高的宝贝</option>";
  options+="<option value='3'>本店销量最低的宝贝</option>";
  //options+="<option value='4'>本店流量最高的宝贝</option>";
  //options+="<option value='5'>本店流量最低的宝贝</option>";
  options+="<option value='12'>本店价格最高的宝贝</option>";
  options+="<option value='13'>本店价格最低的宝贝</option>";
  options+="<option value='15'>同类随机推荐宝贝</option>";
  options+="<option value='16'>同类价格高的宝贝</option>";
  options+="<option value='17'>同类价格低的宝贝</option>";
  $("#intelligence_label_1").html(options);
  
  //var hidden_intelligence_label_1 = $("#hidden_intelligence_label_1").val();
  //$("#intelligence_label_1").val(hidden_intelligence_label_1);
}

function set_intell_labels_normal(){
  var options ="";
  options+="<option value='1'>最近上架的宝贝</option>";
  options+="<option value='11'>最近编辑过的宝贝</option>";
  options+="<option value='2'>本店销量最高的宝贝</option>";
  options+="<option value='3'>本店销量最低的宝贝</option>";
  options+="<option value='12'>本店价格最高的宝贝</option>";
  options+="<option value='13'>本店价格最低的宝贝</option>";
  //options+="<optgroup label='本店流量最高的宝贝[高级版专享]'></optgroup>";
  //options+="<optgroup label='本店流量最低的宝贝[高级版专享]'></optgroup>";
  options+="<optgroup label='同类随机推荐宝贝[高级版专享]'></optgroup>";
  options+="<optgroup label='同类价格高的宝贝[高级版专享]'></optgroup>";
  options+="<optgroup label='同类价格低的宝贝[高级版专享]'></optgroup>";
  $("#intelligence_label_1").html(options);
  
  //var hidden_intelligence_label_1 = $("#hidden_intelligence_label_1").val();
  //$("#intelligence_label_1").val(hidden_intelligence_label_1);
}



function loadGridTable1_2() {
  $("#gridTable").jqGrid({
    url: "items.action"+$("#jsession_id").val()+"?approveStatus="+$("#approve_status").val(),
    datatype: "json",
    height: 650,
    width: 750,
    colNames: ["编号", "图片", "名称", "价格", "下架时间", "pic_url","frank_title"],
    colModel: [{
      name: "iid",
      index: "iid",
      hidden: true
    },
    {
      name: "pic_url",
      index: "pic_url",
      formatter:formatItemPic,
      width: "60",
      align:"center",
      sortable: false
    },
    {
      name: "title",
      index: "title",
      formatter:formateadorLink,
      width: "350"
    },
    {
      name: "price",
      index: "price",
      sorttype: "int",
      width: "60"
    },
    {
      name: "delist_time",
      index: "delist_time",
      width: "160"
    },
    {
      name: "pic_url",
      index: "pic_url",
      hidden: true
    },
    {
      name: "frank_title",
      index: "frank_title",
      hidden: true
    }
    ],
    sortname: "delist_time",
    sortorder: "desc",
    viewrecords: true,
    rowNum: 10,
    rowList: [10, 20, 30],
    multiselect: true,
    multiselectWidth: 50,
    loadComplete: function () {
      setGridTableSelection();
      setImageCellHeight('gridTable');
    },
    onSelectRow: function (rowid, status) {
      $("#accordion").accordion("option", "active", 0);
      if (rowid === null) {
        return;
      }
      var ret = $("#gridTable").jqGrid("getRowData", rowid);
      var oldsrc = $("#" + ret.iid + " img").attr("src");
      if (status) {
        if (oldsrc != null) {
          return;
        }
        var title_html = builderSimpleTitle(ret);
        var onClickButtonHtml = "<input type='button' class='btn2 pbtn1' onmouseout=\"this.style.borderColor='#dcdcdc'\" onmouseover=\"this.style.borderColor='#f76b00'\" value='放弃推广' style=\"border-color: rgb(220, 220, 220);\" onclick=\"onClickDelete_Step1_2('li" + ret.iid + "')\">";
        var result = "<li id='li" + ret.iid + "'><div class='bePromoted' price='" + ret.price + "' id='" + ret.iid +  "' item_title='"+ret.frank_title+"'> <img src='" + ret.pic_url + "'/><br/>" +title_html +"<br/>" +onClickButtonHtml+ "</div>" +"</li>";
        $("#sortable_step1_2").append(result);
      } else {
        $("#" + ret.iid).remove();
        $("#li" + ret.iid).remove();
      }
      transBePromotedToServer();
    },
    onSelectAll: function (ids, status) {
      $("#accordion").accordion("option", "active", 0);
      if (ids === null || ids.length === 0) {
        return;
      }
      if (status) {
        for (var i = 0; i < ids.length; i++) {
          var ret = $("#gridTable").jqGrid("getRowData", ids[i]);
          var oldsrc = $("#" + ret.iid + " img").attr("src");
          if (oldsrc == null) {
            var title_html = builderSimpleTitle(ret);
            var onClickButtonHtml ="<input type='button' class='btn2 pbtn1' onmouseout=\"this.style.borderColor='#dcdcdc'\" onmouseover=\"this.style.borderColor='#f76b00'\" value='放弃推广' style=\"border-color: rgb(220, 220, 220);\" onclick=\"onClickDelete_Step1_2('li" + ret.iid + "')\">";
            var result = "<li id='li" + ret.iid + "'><div class='bePromoted' price='" + ret.price + "' id='" + ret.iid +  "' item_title='"+ret.frank_title+"'> <img src='" + ret.pic_url + "'/><br/>" +title_html+"<br/>"+onClickButtonHtml+"</div>"  + "</li>";
            $("#sortable_step1_2").append(result);
          }
        }
      } else {
        for (i = 0; i < ids.length; i++) {
          ret = $("#gridTable").jqGrid("getRowData", ids[i]);
          $("#" + ret.iid).remove();
          $("#li" + ret.iid).remove();
        }
      }
      transBePromotedToServer();
    },
    jsonReader: {
      root: "dataset",
      page: "pager.curPageNo",
      total: "pager.totalPages",
      records: "pager.totalCount",
      repeatitems: false
    },
    prmNames: {
      rows: "pager.pageSize",
      page: "pager.curPageNo",
      sort: "pager.orderBy",
      order: "pager.order"
    },
    pager: "#gridPager",
    caption: "宝贝列表"
  });
}


function setGridTableSelection() {
  var rownum = $("#gridTable").jqGrid("getGridParam", "rowNum");
  if(rownum==null){
    return;
  }
  $("#gridTable").jqGrid("resetSelection");
  $(".bePromoted").each(function () {
    var iid = $(this).attr("id");
    for (var i = 1; i <= rownum; i++) {
      var ret = $("#gridTable").jqGrid("getRowData", i);
      if (ret.iid == iid) {
        $("#gridTable").setSelection(i);
      }
    }
  });
}

function onClickDelete_Step1_2(id) {
  $("#" + id).remove();
  setGridTableSelection(); // 重新设置表格中的选择
  transBePromotedToServer();
  freshPreview();
}

function doSearch1() {
  var searchKey = $("#item_key").val();
  searchKey = encodeURI(encodeURI(searchKey));
  $("#gridTable").jqGrid("setGridParam", {
    url: "items.action"+$("#jsession_id").val()+"?cid=" + $('#sellerCats').val()+"&approveStatus="+$("#approve_status").val()+"&searchKey="+searchKey,
    page: 1
  }).trigger("reloadGrid");
}

/*
 * 带上refresh参数表示重新从淘宝取数据
 */

function doRefresh1() {
  $("#gridTable").jqGrid("setGridParam", {
    url: "items.action?refresh=true",
    page: 1
  }).trigger("reloadGrid");
}

